{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.be4cde964e90d77d3ffba13cbeeaee1a.315360000.1904891515.282335-19897464', 'expires_in': 2592000, 'session_key': '9mzdCrExHXkp39CKhk7MnNZ16fTHyCbG7G7WTB+0Q9EzrfCU6aOGCzRCQjx1F8rU25GB+Zbr/jDjYABo9JhgQkpMHvIiYw==', 'access_token': '24.15e97a8bc81b2cd4645952d4dc1ac1d9.2592000.1592123515.282335-19897464', 'scope': 'public nlp_simnet nlp_wordemb nlp_comtag nlp_dnnlm_cn brain_nlp_lexer brain_all_scope brain_nlp_comment_tag brain_nlp_dnnlm_cn brain_nlp_word_emb_vec brain_nlp_word_emb_sim brain_nlp_sentiment_classify brain_nlp_simnet brain_nlp_depparser brain_nlp_wordembedding brain_nlp_dnnlm_cn_legacy brain_nlp_simnet_legacy brain_nlp_comment_tag_legacy brain_nlp_lexer_custom brain_nlp_keyword brain_nlp_topic brain_nlp_ecnet brain_nlp_emotion brain_nlp_comment_tag_custom brain_nlp_news_summary brain_nlp_sentiment_classify_custom brain_nlp_address wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理', 'session_secret': '1c5d8b7a33a2ed4aa3ecb0072fa00685'}\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>refresh_token</th>\n",
       "      <td>25.be4cde964e90d77d3ffba13cbeeaee1a.315360000....</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>expires_in</th>\n",
       "      <td>2592000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>session_key</th>\n",
       "      <td>9mzdCrExHXkp39CKhk7MnNZ16fTHyCbG7G7WTB+0Q9Ezrf...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>access_token</th>\n",
       "      <td>24.15e97a8bc81b2cd4645952d4dc1ac1d9.2592000.15...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>scope</th>\n",
       "      <td>public nlp_simnet nlp_wordemb nlp_comtag nlp_d...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>session_secret</th>\n",
       "      <td>1c5d8b7a33a2ed4aa3ecb0072fa00685</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                0\n",
       "refresh_token   25.be4cde964e90d77d3ffba13cbeeaee1a.315360000....\n",
       "expires_in                                                2592000\n",
       "session_key     9mzdCrExHXkp39CKhk7MnNZ16fTHyCbG7G7WTB+0Q9Ezrf...\n",
       "access_token    24.15e97a8bc81b2cd4645952d4dc1ac1d9.2592000.15...\n",
       "scope           public nlp_simnet nlp_wordemb nlp_comtag nlp_d...\n",
       "session_secret                   1c5d8b7a33a2ed4aa3ecb0072fa00685"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=a6NboH9ebGu2dtfoOIBH6ReX&client_secret=y60MKflWI8dWfkwDSWCaGhBtPcDmEhYk'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())\n",
    "pd.json_normalize(response.json()).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.15e97a8bc81b2cd4645952d4dc1ac1d9.2592000.1592123515.282335-19897464'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token = response.json()['access_token']\n",
    "access_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'log_id': 777828637804206927,\n",
       " 'text': '百度是一家高科技公司',\n",
       " 'items': [{'loc_details': [],\n",
       "   'byte_offset': 0,\n",
       "   'uri': '',\n",
       "   'pos': '',\n",
       "   'ne': 'ORG',\n",
       "   'item': '百度',\n",
       "   'basic_words': ['百度'],\n",
       "   'byte_length': 4,\n",
       "   'formal': ''},\n",
       "  {'loc_details': [],\n",
       "   'byte_offset': 4,\n",
       "   'uri': '',\n",
       "   'pos': 'v',\n",
       "   'ne': '',\n",
       "   'item': '是',\n",
       "   'basic_words': ['是'],\n",
       "   'byte_length': 2,\n",
       "   'formal': ''},\n",
       "  {'loc_details': [],\n",
       "   'byte_offset': 6,\n",
       "   'uri': '',\n",
       "   'pos': 'm',\n",
       "   'ne': '',\n",
       "   'item': '一家',\n",
       "   'basic_words': ['一', '家'],\n",
       "   'byte_length': 4,\n",
       "   'formal': ''},\n",
       "  {'loc_details': [],\n",
       "   'byte_offset': 10,\n",
       "   'uri': '',\n",
       "   'pos': 'n',\n",
       "   'ne': '',\n",
       "   'item': '高科技',\n",
       "   'basic_words': ['高', '科技'],\n",
       "   'byte_length': 6,\n",
       "   'formal': ''},\n",
       "  {'loc_details': [],\n",
       "   'byte_offset': 16,\n",
       "   'uri': '',\n",
       "   'pos': 'n',\n",
       "   'ne': '',\n",
       "   'item': '公司',\n",
       "   'basic_words': ['公司'],\n",
       "   'byte_length': 4,\n",
       "   'formal': ''}]}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "BASE_URL = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer' \n",
    "params = {\"access_token\":access_token}\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "data = {\"text\": \"百度是一家高科技公司\"}\n",
    " \n",
    "r = requests.post(BASE_URL, params=params, headers=headers,json = data)\n",
    "r.status_code\n",
    "r.content\n",
    "results = r.json() # \n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-32-e70ebabd2aac>, line 9)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-32-e70ebabd2aac>\"\u001b[1;36m, line \u001b[1;32m9\u001b[0m\n\u001b[1;33m    return request.post(url,params = params,headers = headers,data = encode_data)\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "def baidu_lexer(text):\n",
    "    url = \"https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer\"\n",
    "    params = {\n",
    "        \"access_token\":access_token\n",
    "    }\n",
    "    headers={'Content-Type': 'application/json'}\n",
    "    data = {\"text\":text}\n",
    "    encode_data=json.dumps(data.encode(\"UTF-8\")\n",
    "                           \n",
    "return request.post(url,params = params,headers = headers,data = encode_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "           "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
